package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.MineManagerActivity;
import com.wude.guild.R;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public class RenZhengFragment extends TitleBaseFragment implements View.OnClickListener {
    private Unbinder unbinder;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idcard)
    EditText etIdCard;
    @BindView(R.id.ok)
    TextView btnOk;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvIdCard)
    TextView tvIdCard;
    @BindView(R.id.tvHint)
    TextView tvHInt;

    private String idCard, realName;
    private String token;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_ren_zheng, container, false);
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("RenZhengFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("RenZhengFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        setHeaderTitle("实名认证");
        UserInfo loginUser = Utils.getLoginUser();
        token = loginUser.token;
        if (loginUser.age_status == 2 || loginUser.age_status == 3) {       //判断如果已经提交姓名身份证，则只能显示个人信息不能修改
            btnOk.setVisibility(View.INVISIBLE);
            etName.setVisibility(View.GONE);
            etIdCard.setVisibility(View.GONE);
            tvHInt.setVisibility(View.GONE);
            tvName.setVisibility(View.VISIBLE);
            tvIdCard.setVisibility(View.VISIBLE);
            tvName.setText(loginUser.real_name);
            tvIdCard.setText(loginUser.idcard.substring(0, 3) + "********" + loginUser.idcard.substring(11));
        }
        btnOk.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private MineManagerActivity activity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MineManagerActivity) activity;
    }

    @Override
    public void onClick(View view) {
        Verification();
    }

    /**
     * 验证输入框是否为空，格式是否正确 的方法
     */
    private void Verification() {
        if (EmptyUtils.isEmpty(etName.getText().toString())) {
            ToastUtil.showToast("请输入姓名");
            return;
        }
        if (!RegexUtils.isZh(etName.getText().toString())) {
            ToastUtil.showToast("姓名格式有误");
            return;
        }
        if (EmptyUtils.isEmpty(etIdCard.getText().toString())) {
            ToastUtil.showToast("请输入身份证号");
            return;
        }
        if (!(RegexUtils.isIDCard15(etIdCard.getText().toString()) || RegexUtils.isIDCard18(etIdCard.getText().toString()))) {
            ToastUtil.showToast("身份证号码格式有误");
            return;
        }
        idCard = etIdCard.getText().toString();
        realName = etName.getText().toString();
        Map<String, String> map = new HashMap<>();
        map.put("idcard", Utils.getBase64String(idCard.getBytes(), 0));
        map.put("real_name", realName);
        map.put("token", token);
        HttpCom.POST(mHandler, HttpCom.getRenZheng(), map, false);
    }

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    boolean i = HttpUtils.DNSYanZheng(msg.obj.toString());
                    if (i) {
                        //loginUser.age_status = 3;
                        activity.popTopFragment(null);
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
                default:
                    break;
            }
        }
    };
}
